package com.fh.service.system.organization;

import com.fh.dao.DaoSupport;
import com.fh.model.Page;
import com.fh.model.system.Organization;
import com.fh.service.runbatch.RunBatchServiceImpl;
import com.fh.util.PageData;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dell、 on 2017/5/20.
 */
@Service("organizationService")
public class OrganizationServiceImpl {
	
	private Logger loger = LoggerFactory.getLogger(OrganizationServiceImpl.class);
	
    @Resource(name = "daoSupport")
    private DaoSupport dao;

    private String nameSpace = "OrganizationMapper";

    /**
     * 查找所有的组织
     * @return
     * @throws Exception
     */
    public List<Organization> findAll() throws Exception {
        return (List<Organization>)dao.findAll(nameSpace + ".findAll");//map的nameSpace
    }

    /**
     * 根据id查找对于组织机构
     * @param organization
     * @return
     * @throws Exception
     */
    public Organization findById(Organization organization) throws Exception {
        return (Organization)dao.findOne(nameSpace + ".findOrganizationById",organization);
    }
    /**
     * 修改分组分配时间
     * @param page
     * @throws Exception
     */
	public void updateByOrganization(Integer S1Max,Integer S2Min,Integer S2Max,Integer S3) throws Exception{
		Organization organization = new Organization();
		if (check(S1Max) || check(S2Min)||  check(S2Max) || check(S3) || S1Max <= 1 || S2Min <= S1Max || S2Max <= S2Min || S3 <= S2Max) 
			throw new RuntimeException("请输入正确数据");
		try {
			organization.setMinDayNum(1);
			organization.setMaxDayNum(S1Max);
			organization.setName("S1");
			dao.update(nameSpace + ".updateByOrganization", organization);
			organization.setMinDayNum(S2Min);
			organization.setMaxDayNum(S2Max);
			organization.setName("S2");
			dao.update(nameSpace + ".updateByOrganization", organization);
			organization.setMinDayNum(S3);
			organization.setMaxDayNum(999);
			organization.setName("S2+");
			dao.update(nameSpace + ".updateByOrganization", organization);
		} catch (Exception e) {
			loger.error("修改updateByOrganization分组异常", e);
			throw new RuntimeException("修改分组异常");
		}
	}
	
	public Boolean check(Integer s){
		return s == null;
	}
}
